package com.jinqing.wuchuang.controller;

import ch.qos.logback.classic.Logger;
import com.jinqing.wuchuang.config.JwtLoginFilter;
import com.jinqing.wuchuang.entity.SettleCompany;
import com.jinqing.wuchuang.entity.SettledLegalRepresentative;
import com.jinqing.wuchuang.entity.SettledTeamMenber;
import com.jinqing.wuchuang.response.RespBean;
import com.jinqing.wuchuang.service.AuthUserService;
import com.jinqing.wuchuang.service.WordService;
import com.jinqing.wuchuang.utils.WordUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 用于测试
 */
@RestController
@Slf4j
public class HelloController {
    private final static org.slf4j.Logger logger= LoggerFactory.getLogger(HelloController.class);
    @Autowired
    private AuthUserService authService;

    @Autowired
    private WordService wordService;

    @GetMapping("hello")
    public RespBean hello(){
        return RespBean.ok("success");
    }


    @GetMapping("dba")
    @PreAuthorize("hasRole('write')")
    public RespBean dba(){
        /**
         * 可以成功访问用户的uid
         */
        String userId =(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return RespBean.ok("成功,使用write权限"+authService.getUidByuserName(userId));
    }
    @GetMapping("stu")
    @PreAuthorize("hasRole('ROLE_stu')")
    public RespBean user(){
        return RespBean.ok("成功，以学生的身份访问");
    }
    @GetMapping("write")
    @PreAuthorize("hasAuthority('write')")
    public RespBean write(){
        return RespBean.ok("成功，用于write权限");
    }

    @GetMapping("temp")
    public RespBean testTemp(HttpServletResponse response){
//        String result = null;
//        SettledLegalRepresentative rep = new SettledLegalRepresentative();
//        rep.setName("唐昊");
//        rep.setTel("184770300000");
//        rep.setBirth("2020年12月15日");
//        rep.setAddress("武汉理工大学马房山校区西院");
//        rep.setNationality("汉");
//        rep.setPersonalProfile("上周，技术圈被 log4j2 漏洞掀起巨浪，各大安全公司纷纷发文介绍该漏洞的危害，并给出了各种临时解决方案。还有一些博主也发表文章教我们如何找到易受攻击的地方，并采取相应的防御措施。还有大量帖子跟着起哄，讨论如何采用一些不必要的防御技术");
//        rep.setIdentityNumber("45088100000000000X");
//        rep.setGender("男");
//        rep.setPoliticalStatus("积极分子");
//        rep.setIsGraduate("在校生");
//        List<SettledTeamMenber> menbers = new ArrayList<>();
//        for (int i = 0;i < 4; i++){
//            SettledTeamMenber menber = new SettledTeamMenber();
//            menber.setAge("1"+i);
//            menber.setMajor("打工人"+i);
//            menber.setTel("1847703000"+i);
//            menber.setDuty("打工"+i);
//            menber.setName("唐"+i);
//            menbers.add(menber);
//        }
//        SettleCompany company = new SettleCompany();
//        company.setName("打工厂");
//        company.setRegistrationTime(new Date().toString());
//        company.setRegisteredCapital("100w");
//        company.setRegisteredAddress("武汉理工大学自动化学院");
//        company.setBusinessLicense("1289y4932hakbkjdbka");
//        company.setOrganizationCode("kajscal821893e");
//        company.setIsRegister("已注册");
//        company.setIndustry("其他的");
//        company.setDreamWork("陶瓷艺术梦工场");
//        company.setProjectDescription("漏洞掀起巨浪，各大安全公司纷纷发文介绍该漏洞的危害，并给出了各种临时解决方案。还有一些博主也发表文章教我们如何找到易受");
//        try {
//            Map params = wordService.creatWordParam(rep,company,menbers);
//            WordUtil.createWord(response,params);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return RespBean.ok(result);
        String result = null;
        try {
            result = wordService.creatWordURL(1472492362556637186L, 5);
            log.info(result);
        } catch (IOException e) {
            log.error("发生错误",e);
        }
        return RespBean.ok(result);
    }
    @GetMapping("testCreatWord")
    public RespBean getWord(){
            String result = null;
            try {
                result = wordService.creatWordURL(1469514397138309122L,4);
                System.out.println(result);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("发生错误",e);
            }
            return RespBean.ok(result);
        }
    }


